package com.apestech.framework.xml.condition.node;

import com.apestech.formwork.xml.XmlQuery;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.xml.condition.modul.Report;
import com.apestech.framework.xml.plugin.XmlPaste;
import com.apestech.framework.xml.pub.core.ElementUtil;
import org.dom4j.Element;

import java.util.List;

/**
 * @author hup
 * @create 2019-04-16-15:51
 */
public class NodeReport extends ElementUtil{
    private Element root;
    private Element node;
    private Report report;
    private XmlQuery xmlQuery;

    public NodeReport(XmlQuery xmlQuery, Element node){
        this.xmlQuery=xmlQuery;
        this.node=node;
        this.root=xmlQuery.getRoot();
    }

    public void getData() throws Exception {
        if(report==null) report=new Report();
        JSONObject attr=getNodeAttr(node).getJSONObject("attrNode");
        report.setBase(attr);
        List<?> list=node.content();
        int len=list.size();
        for(int i=0; i<len; i++){
            String scalss=list.get(i).getClass().getName();
            if(scalss.equals("org.dom4j.tree.DefaultElement")){
                Element ele=(Element) list.get(i);
                String name=ele.getName();
                switch(name){
                    case "tables":{
                        XmlPaste xml=new XmlPaste(xmlQuery.getSession());
                        xml.expain(root, ele);
                        report.setTable(xml.getTable());;
                        break;
                    }
                    case "generate":{
                        getGenerate(ele);
                        break;
                    }
                    case "iniUpdate":{
                        getFinalUpdate(ele, true);
                        break;
                    }
                    case "finalUpdate":{
                        getFinalUpdate(ele, false);
                        break;
                    }
                }
            }
        }
    }

    private void getFinalUpdate(Element e, boolean init) throws Exception {
        List<?> list=e.content();
        int len=list.size();
        for(int i=0; i<len; i++) {
            String scalss = list.get(i).getClass().getName();
            if (scalss.equals("org.dom4j.tree.DefaultElement")) {
                Element eml = (Element) list.get(i);
                String type=eml.getName();
                JSONObject attr=getNodeAttr(eml).getJSONObject("attrNode");
                if(attr==null) attr=new JSONObject();
                if(!"".equals(eml.getTextTrim())){
                    attr.put("sql", eml.getTextTrim());
                }
                attr.put("type", type);
                if(init) report.addIniUpdate(attr);
                else report.addFinalUpdate(attr);
            }
        }
    }

    private void getGenerate(Element e) throws Exception {
        JSONObject attr=getNodeAttr(e).getJSONObject("attrNode");
        List<?> list=e.content();
        int len=list.size();
        for(int i=0; i<len; i++) {
            String scalss = list.get(i).getClass().getName();
            int inx=0;
            if (scalss.equals("org.dom4j.tree.DefaultElement")) {
                Element eml = (Element) list.get(i);
                String name=eml.getName();
                switch(name){
                    case "empty":{
                        if(attr==null) attr=new JSONObject();
                        attr.put("empty", eml.getTextTrim());
                        report.addGenerate(attr);
                        break;
                    }
                    case "modulSql":{
                        getModulSql(eml);
                        break;
                    }
                    case "line":{
                        getColumn(inx, eml);
                        inx++;
                        break;
                    }
                    case "sum":{
                        getSum(eml);
                        break;
                    }
                    case "fields":{
                        getFields(eml);
                        break;
                    }
                }
            }
        }
    }

    private void getSum(Element e) throws Exception {
        List<?> list=e.content();
        int len=list.size();
        for(int i=0; i<len; i++) {
            String scalss = list.get(i).getClass().getName();
            if (scalss.equals("org.dom4j.tree.DefaultElement")) {
                Element eml = (Element) list.get(i);
                JSONObject attr=getNodeAttr(eml).getJSONObject("attrNode");
                attr.put("sql", eml.getTextTrim());
                report.addSumColumn(attr);
            }
        }
    }

    private void getModulSql(Element e){
        List<?> list=e.content();
        int len=list.size();
        for(int i=0; i<len; i++) {
            String scalss = list.get(i).getClass().getName();
            if (scalss.equals("org.dom4j.tree.DefaultElement")) {
                Element eml = (Element) list.get(i);
                report.addModeulSql(eml.getName(), eml.getTextTrim());
            }
        }
    }

    private void getFields(Element e) throws Exception {
        List<?> list=e.content();
        int len=list.size();
        for(int i=0; i<len; i++) {
            String scalss = list.get(i).getClass().getName();
            if (scalss.equals("org.dom4j.tree.DefaultElement")) {
                Element eml = (Element) list.get(i);
                report.addFields(getFieldsInfo(eml));
                //report.addModeulSql(eml.getName(), eml.getTextTrim());
                //这个结构是JSONArray
            }
        }
    }

    /*
    <field type="condition" id="T03" inputs="GSID;SSGSID">
        <BL result="P001" />
        <JE result="P002" />
    </field>
    */
    private Report.Fields getFieldsInfo(Element e) throws Exception {
        JSONObject m=getNodeAttr(e).getJSONObject("attrNode");
        m.put("express", e.getTextTrim());
        Report.Fields field=report.newFields(m);
        List<?> list=e.content();
        int len=list.size();
        for(int i=0; i<len; i++) {
            String scalss = list.get(i).getClass().getName();
            if (scalss.equals("org.dom4j.tree.DefaultElement")) {
                Element eml = (Element) list.get(i);
                JSONObject m1=getNodeAttr(eml).getJSONObject("attrNode");
                m1.put("field", eml.getName());
                m1.put("express", eml.getTextTrim());
                field.addOutField(m1);
            }
        }
        return field;
    }

    private void getColumn(int index, Element e) throws Exception {
        List<?> list=e.content();
        int len=list.size();
        for(int i=0; i<len; i++) {
            String scalss = list.get(i).getClass().getName();
            if (scalss.equals("org.dom4j.tree.DefaultElement")) {
                Element eml = (Element) list.get(i);
                JSONObject m=getNodeAttr(eml).getJSONObject("attrNode");
                if(m==null) m=new JSONObject();
                m.put("sql", eml.getTextTrim());
                report.addColumn(index, m.getString("id"), m);
            }
        }
    }

    public Report getReport() {
        return report;
    }
}
